﻿# If army or navy are currently used in diplomatic play, their expenses will be higher than usual. Many actions are
# affected by this or by country being at war in general. To improve stability of country management a timed variable
# is used. When war ends, decision making returns to peaceful mode not in the next iteration, but in the next + 1.
aroai_check_if_military_expenses_are_higher_than_usual = {
    if = {
        limit = {
            aroai_is_using_military_forces = yes
        }
        aroai_renew_variable = {
            name = aroai_military_expenses_are_higher_than_usual
            days = aroai_days_in_the_iteration_plus_week
        }
    }
}

# Custom management of budget settings
aroai_manage_tax_and_wage_level = {
    if = {
        limit = {
            has_variable = aroai_country_budget_surplus
            is_player = no
            NOT = {
                has_variable = aroai_budget_settings_cooldown
            }
        }
        
        # Budget is neutral or positive: lower taxes and raise wages.
        # Priorities:
        # 1. Ensure tax level is not very high.
        # 2. Ensure wage level is not below medium.
        # 3. Gradually lower tax level to low.
        # 4. Gradually raise wages to very high.
        # 5. Lower taxt level to very low.
        if = {
            limit = {
                aroai_budget_health_is_equal_or_higher = {
                    level = 0
                }

                # Don't do it if construction expenses are far smaller than planned
                # TODO this doesn't work cause sector spending need later data
                # aroai_building_construction_sector_spending_value >= aroai_building_construction_sector_spending_1
            }
            
            # Raise wages.
            # If taxes are on very high level, then lowering taxes is the priority so we skip this.
            if = {
                limit = {
                    aroai_tax_level_equal_or_lower = {
                        value = 4
                    }
                }

                # Government wages
                if = {
                    limit = {
                        aroai_government_wages_are_very_low = yes
                    }
                    set_government_wage_level = low
                    set_local_variable = aroai_budget_settings_cooldown
                }
                else_if = {
                    limit = {
                        aroai_government_wages_are_low = yes
                    }
                    set_government_wage_level = medium
                    set_local_variable = aroai_budget_settings_cooldown
                }
                else_if = {
                    limit = {
                        aroai_budget_health_is_equal_or_higher = {
                            level = 1
                        }
                        aroai_tax_level_equal_or_lower = {
                            value = 2
                        }
                    }
                    if = {
                        limit = {
                            aroai_government_wages_are_medium = yes
                        }
                        set_government_wage_level = high
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                    else_if = {
                        limit = {
                            aroai_government_wages_are_high = yes
                        }
                        set_government_wage_level = very_high
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                }

                # Military wages
                if = {
                    limit = {
                        aroai_military_wages_are_very_low = yes
                    }
                    set_military_wage_level = low
                    set_local_variable = aroai_budget_settings_cooldown
                }
                else_if = {
                    limit = {
                        aroai_military_wages_are_low = yes
                    }
                    set_military_wage_level = medium
                    set_local_variable = aroai_budget_settings_cooldown
                }
                else_if = {
                    limit = {
                        aroai_budget_health_is_equal_or_higher = {
                            level = 1
                        }
                        aroai_tax_level_equal_or_lower = {
                            value = 2
                        }
                    }
                    if = {
                        limit = {
                            aroai_military_wages_are_medium = yes
                        }
                        set_military_wage_level = high
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                    else_if = {
                        limit = {
                            aroai_military_wages_are_high = yes
                        }
                        set_military_wage_level = very_high
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                }
            }
            
            # Lower taxes.
            # If wages were raised, skip this as we don't wanna change too much.
            if = {
                limit = {
                    NOT = {
                        has_local_variable = aroai_budget_settings_cooldown
                    }
                }
                if = {
                    limit = {
                        tax_level = very_high
                    }
                    set_tax_level = high
                    set_local_variable = aroai_budget_settings_cooldown
                }
                else_if = {
                    limit = {
                        aroai_budget_health_is_equal_or_higher = {
                            level = 2
                        }
                    }
                    if = {
                        limit = {
                            tax_level = high
                        }
                        set_tax_level = medium
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                    else_if = {
                        limit = {
                            tax_level = medium
                        }
                        set_tax_level = low
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                    else_if = {
                        limit = {
                            tax_level = low
                            aroai_government_wages_are_very_high = yes
                            aroai_military_wages_are_very_high = yes
                        }
                        set_tax_level = very_low
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                }
            }
        }
        
        # Budget is below neutral: raise taxes and lower wages.
        # Priorities:
        # 1. Ensure tax level is not very low.
        # 2. Ensure wage level is not above medium.
        # 3. Gradually raise tax level to high.
        # 4. Gradually lower wage level to very low.
        # Sidenotes:
        # – Military wages are always set to medium during wars.
        # – Wages may be raised from very low to low if the situation is not that bad.
        else_if = {
            limit = {
                NOT = {
                    aroai_budget_health_is_equal_or_higher = {
                        level = 0
                    }
                }
            }
            
            # Raise taxes
            if = {
                limit = {
                    tax_level = very_low
                }
                set_tax_level = low
                set_local_variable = aroai_budget_settings_cooldown
            }
            
            # Lower wages
            else_if = {
                limit = {
                    OR = {
                        aroai_government_wages_are_very_high = yes
                        aroai_military_wages_are_very_high = yes
                    }
                }
                set_government_wage_level = high
                set_military_wage_level = high
                set_local_variable = aroai_budget_settings_cooldown
            }
            else_if = {
                limit = {
                    OR = {
                        aroai_government_wages_are_high = yes
                        aroai_military_wages_are_high = yes
                    }
                }
                set_government_wage_level = medium
                set_military_wage_level = medium
                set_local_variable = aroai_budget_settings_cooldown
            }
            else = {
                
                # Raise wages is they are very low but budget health is not that bad
                if = {
                    limit = {
                        aroai_budget_health_is_equal_or_higher = {
                            level = -2
                        }
                        aroai_tax_level_equal_or_higher = {
                            value = 4
                        }
                    }
                    if = {
                        limit = {
                            OR = {
                                aroai_government_wages_are_very_low = yes
                                aroai_can_cut_government_wages = yes
                            }
                        }
                        set_government_wage_level = low
                    }
                    if = {
                        limit = {
                            OR = {
                                aroai_military_wages_are_very_low = yes
                                aroai_can_cut_military_wages = yes
                            }
                        }
                        set_military_wage_level = low
                    }
                }

                # Lower wages
                else_if = {
                    limit = {
                        NOT = {
                            aroai_budget_health_is_equal_or_higher = {
                                level = -2
                            }
                        }
                        aroai_tax_level_equal_or_higher = {
                            value = 4
                        }
                    }
                    if = {
                        limit = {
                            aroai_government_wages_are_low = yes
                            aroai_can_cut_government_wages = yes
                        }
                        set_government_wage_level = very_low
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                    if = {
                        limit = {
                            aroai_military_wages_are_low = yes
                            aroai_can_cut_military_wages = yes
                        }
                        set_military_wage_level = very_low
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                }
                
                # Force military wages when in conflict
                if = {
                    limit = {
                        aroai_is_using_military_forces = yes
                    }
                    set_military_wage_level = medium
                }
                
                # Raise taxes
                if = {
                    limit = {
                        NOT = {
                            has_local_variable = aroai_budget_settings_cooldown
                        }
                    }
                    if = {
                        limit = {
                            aroai_budget_health_is_equal_or_higher = {
                                level = -3
                            }
                            aroai_tax_level_equal_or_lower = {
                                value = 3
                            }
                        }
                        if = {
                            limit = {
                                tax_level = very_low
                            }
                            set_tax_level = low
                            set_local_variable = aroai_budget_settings_cooldown
                        }
                        else_if = {
                            limit = {
                                tax_level = low
                            }
                            set_tax_level = medium
                            set_local_variable = aroai_budget_settings_cooldown
                        }
                        else_if = {
                            limit = {
                                tax_level = medium
                            }
                            set_tax_level = high
                            set_local_variable = aroai_budget_settings_cooldown
                        }
                    }
                    else_if = {
                        limit = {
                            NOT = {
                                aroai_budget_health_is_equal_or_higher = {
                                    level = -3
                                }
                            }
                            aroai_tax_level_equal_or_lower = {
                                value = 4
                            }
                        }
                        set_tax_level = very_high
                        set_local_variable = aroai_budget_settings_cooldown
                    }
                }
            }
        }

        # Local variable into timed variable
        if = {
            limit = {
                has_local_variable = aroai_budget_settings_cooldown
            }
            set_variable = {
                name = aroai_budget_settings_cooldown
                value = yes
                days = aroai_days_in_the_iteration_plus_week
            }
            remove_local_variable = aroai_budget_settings_cooldown
        }
    }
}